package com.yiren.dbaa.module.practice_set.line.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.module.practice_set.line.contract.LineManageContract;
import com.yiren.dbaa.module.practice_set.line.presenter.LineManagePresenter;
import com.yiren.dbaa.module.practice_set.site.view.dialog.SelectUserDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/11/16 14:31
 * @description: 线路管理页
 */
public class LineManageActivity extends BaseActivity implements LineManageContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_line_name)
    TextView tvLineName;
    @BindView(R.id.tv_line_version)
    TextView tvLineVersion;
    @BindView(R.id.tv_line_version_bt)
    TextView tvLineVersionBt;
    @BindView(R.id.tv_using_range)
    TextView tvUsingRange;
    @BindView(R.id.tv_using_range_bt)
    TextView tvUsingRangeBt;

    private Intent intent;
    private LineInfoEntity line;
    private LineManageContract.Presenter lineManagePresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_line);
        ButterKnife.bind(this);
        tvTitle.setText("线路数据管理");
        intent = getIntent();
        line = (LineInfoEntity) intent.getSerializableExtra("line");
        init();
    }

    private void init() {
        lineManagePresenter = new LineManagePresenter(this);
        if (line != null) {
            tvLineName.setText(line.getName());
            tvLineVersion.setText("V" + line.getLineVer() + "");
            if (line.getUseRange() == 1) {
                tvUsingRange.setText("个别用户使用");
            } else {
                tvUsingRange.setText("全部用户使用");
            }
            if (line.getLineVer() != 0) {
                tvLineVersionBt.setText("回退版本");
                tvLineVersionBt.setTextColor(getResources().getColor(R.color.white));
                tvLineVersionBt.setBackgroundResource(R.drawable.btn_blue_radius_selector);
                if (line.getUseRange() == 1) {
                    tvUsingRangeBt.setText("修改范围");
                    tvUsingRangeBt.setTextColor(getResources().getColor(R.color.white));
                    tvUsingRangeBt.setBackgroundResource(R.drawable.btn_blue_radius_selector);
                } else {
                    tvUsingRangeBt.setText("已是全部");
                    tvUsingRangeBt.setTextColor(getResources().getColor(R.color.gray_text));
                }
            } else {
                tvUsingRange.setText("无用户使用");
            }
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_line_version_bt, R.id.tv_using_range_bt, R.id.tv_release, R.id.tv_edit_line})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_line_version_bt:
                intent = new Intent(this, LineHistoryActivity.class);
                Bundle bundle2 = new Bundle();
                bundle2.putSerializable("line", line);
                intent.putExtras(bundle2);
                startActivity(intent);
                break;
            case R.id.tv_using_range_bt:
                if (line.getUseRange() == 1) {
                    List<String> user = new ArrayList<>();
                    user.add("全部用户使用");
                    user.add("个别用户使用");
                    SelectUserDialog.show(this, "修改范围", line.getUseRange() == 1 ? 1 : 0, user, new SelectUserDialog.OnSureClickListener() {
                        @Override
                        public void onSureClick(int selectIndex) {
                            lineManagePresenter.changeUserRange(LineManageActivity.this, line, selectIndex == 0 ? 2 : 1);
                        }
                    });
                }
                break;
            case R.id.tv_release:
                intent = new Intent(this, LineReleaseActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("line", line);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.tv_edit_line:
                intent = new Intent(this, LineEditActivity.class);
                Bundle bundle1 = new Bundle();
                bundle1.putSerializable("line", line);
                intent.putExtra("pageType", 2);
                intent.putExtras(bundle1);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void updateUseRange(int range) {
        if (range == 2) {
            tvUsingRange.setText("全部用户使用");
            tvUsingRangeBt.setText("已是全部");
            tvUsingRangeBt.setBackground(null);
            tvUsingRangeBt.setTextColor(getResources().getColor(R.color.gray_text));
        } else {
            tvUsingRange.setText("个别用户使用");
        }

    }
}
